import 'package:flutter/material.dart';
import 'package:flutter_default_project/models.dart';
import 'package:flutter_default_project/todo_list_model.dart';
import 'package:provider/provider.dart';

class AddTodoScreen extends StatefulWidget {
  const AddTodoScreen({super.key});

  @override
  State<AddTodoScreen> createState() => _AddTodoScreenState();
}

class _AddTodoScreenState extends State<AddTodoScreen> {
  final _formKey = GlobalKey<FormState>();
  final _titleController = TextEditingController();
  final _noteController = TextEditingController();
  @override
  Widget build(BuildContext context) {
    final textTheme = Theme.of(context).textTheme;
    return Scaffold(
      appBar: AppBar(title: const Text('Add Todo')),
      body: Form(
        key: _formKey,
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            children: [
              TextFormField(
                controller: _titleController,
                style: TextStyle(fontSize: 24),
                decoration: InputDecoration(hintText: 'hello task'),
                validator: (val) {
                  return val!.trim().isEmpty ? 'Please enter a title' : null;
                },
              ),
              TextFormField(
                controller: _noteController,
                style: textTheme.bodyMedium,
                decoration: InputDecoration(hintText: 'hello note'),
                maxLines: 10,
              ),
            ],
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          if (_formKey.currentState!.validate()) {
            // final todolistModel = Provider.of<TodoListModel>(
            //   context,
            //   // listen: false,
            // );

            // todolistModel.addTodo(
            //   Todo(_titleController.text, note: _noteController.text),
            // );
            final todo = Todo(
              _titleController.text,
              note: _noteController.text,
            );
            context.read<TodoListModel>().addTodo(todo);
            Navigator.pop(context);
          }
        },
        child: Icon(Icons.add),
      ),
    );
  }
}
